package com.briup.smartcity_group6.service.Impl;

import com.briup.smartcity_group6.bean.basic.Article;
import com.briup.smartcity_group6.bean.basic.BaseUser;
import com.briup.smartcity_group6.bean.basic.Category;
import com.briup.smartcity_group6.bean.basic.Comment;
import com.briup.smartcity_group6.dao.basic.ArticleMapper;
import com.briup.smartcity_group6.dao.basic.BaseUserMapper;
import com.briup.smartcity_group6.dao.basic.CategoryMapper;
import com.briup.smartcity_group6.dao.basic.CommentMapper;
import com.briup.smartcity_group6.dao.ext.ArticleExtMapper;
import com.briup.smartcity_group6.service.IArticleService;
import com.briup.smartcity_group6.web.vo.ArticleVO;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    @Autowired
    private ArticleExtMapper articleExtMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private BaseUserMapper baseUserMapper;

    //通过id查新所有的咨询信息，同时获取咨询对应的栏目和用户信息
    public ArticleVO findByIdWithCategoryAndUser(int id) {
        Article article1=articleMapper.selectByPrimaryKey(id);
        Integer catrgoryId=  articleExtMapper.findCategoryIdByArticleId(id);
        Integer userId = articleExtMapper.findUserIdByArticleId(id);
        Integer commentId = articleExtMapper.findCommentIdByArticleId(id);
        Category category=categoryMapper.selectByPrimaryKey(catrgoryId);
        BaseUser baseUser=baseUserMapper.selectByPrimaryKey(userId);
        Comment comment=commentMapper.selectByPrimaryKey(commentId);
        ArticleVO article= new ArticleVO(article1,baseUser,comment,category);
        return article;
    }

    @Override
    public void saveOrUpdate(Article article) {
        if (!(article.getArticleId()==null)){
            articleMapper.insert(article);
        }
        Article article1=articleMapper.selectByPrimaryKey(article.getArticleId());
        if (!(article1==null)){
           articleMapper.updateByPrimaryKey(article);
        }
    }


    @Override
    public PageInfo<ArticleVO> pageQueryArticleWithCategoryAndUser(int pageNum, int
            pageSize, String title, Integer status, Integer categoryId) {
        List<Integer> list=articleExtMapper.selectArticleIdWithUserAndTitleAndStatus(title, status,  categoryId);
        List<ArticleVO> articleVOList=new ArrayList<>();
        for (int i = 0; i < list.size() ; i++) {
            Article article1=articleMapper.selectByPrimaryKey(list.get(i));
            Integer commentId = articleExtMapper.findCommentIdByArticleId(list.get(i));
            Category category=categoryMapper.selectByPrimaryKey(list.get(i));
            BaseUser baseUser=baseUserMapper.selectByPrimaryKey(list.get(i));
            Comment comment=commentMapper.selectByPrimaryKey(commentId);
            ArticleVO article= new ArticleVO(article1,baseUser,comment,category);
            articleVOList.add(article);
        }
        //设置分页信息
        PageHelper.startPage(pageNum,pageSize,true);
        //执行分页查询
        PageInfo<ArticleVO> pageInfo=new PageInfo<>(articleVOList);
        return pageInfo;

    }

    @Override
    public void deleById(Integer id) {
        articleMapper.deleteByPrimaryKey(id);
    }
    //java8写法：
    //PageInfo<Category> pageInfo = PageHelper.startPage(PageNum, PageSize, true).doSelectPageInfo(
    //        () -> categoryExtMapper.selectAll()
    //);
    //    return pageInfo;
    @Override
    public PageInfo<ArticleVO> pageQueryRecommend(int pageNum, int pageSize) {
        List<ArticleVO> articleVOList=new ArrayList<>();
        List<Integer> list=articleExtMapper.findIdBystatus();
        for (int i = 0; i < list.size() ; i++) {
            Article article1=articleMapper.selectByPrimaryKey(list.get(i));
            Integer commentId = articleExtMapper.findCommentIdByArticleId(list.get(i));
            Category category=categoryMapper.selectByPrimaryKey(list.get(i));
            BaseUser baseUser=baseUserMapper.selectByPrimaryKey(list.get(i));
            Comment comment=commentMapper.selectByPrimaryKey(commentId);
            ArticleVO article= new ArticleVO(article1,baseUser,comment,category);
            articleVOList.add(article);
        }
        //设置分页信息
        PageHelper.startPage(pageNum,pageSize,true);
        //执行分页查询
        PageInfo<ArticleVO> pageInfo=new PageInfo<>(articleVOList);
        return pageInfo;
    }

    @Override
    public void changeStatus(Integer id, Integer status) {
    //    状态,0-待审核，1-不通过，2-通过，3-推荐
        articleExtMapper.changeStatusById(id,status);
    }
}
